﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using System.Linq;
using DTO;
using BLL;
namespace CRM.GUI.QLCuocGoi
{
    public partial class frmCallDetail : DevExpress.XtraEditors.XtraForm
    {
        private List<ResultStatusEntity> listRSE;
        private int _CallStatus;
        private CustomerEntity _Cus;
        private BLsupervisor sup = new BLsupervisor();
        private UserInfoEntity us = new UserInfoEntity();
        private CallManagement cm = new CallManagement();
        private CallMonitor call = new CallMonitor();
        private BindingSource bsTList = new BindingSource();
        private BindingSource bsCus = new BindingSource();
        private CallDetailEntity _calldetail;
        private int _currentrow;

        public frmCallDetail()
        {         
            CallManagement cm = new CallManagement();
            InitializeComponent();
            IntialConfig();
            RefreshCAlist();
            IntialLoad();            
            grbAddChild.Hide();
            grbChildInformation.Hide();
            xtcDetail.Hide();
            listRSE = SharedData.ResultStatusList;
            
        }

        private void IntialConfig()
        {            
            grdViewTaskCA.OptionsView.ColumnAutoWidth = false;
            grdViewTaskCA.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewTaskCA.HorzScrollVisibility = ScrollVisibility.Always;
            grdViewTaskCA.OptionsSelection.MultiSelect = false;
            grdViewTaskCA.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gridcolCount.UnboundType = DevExpress.Data.UnboundColumnType.Integer;
            grdViewTaskCA.CustomUnboundColumnData += grdViewTaskCA_CustomUnboundColumnData;

            grdViewHistoryInformation.BestFitColumns();
            grdViewHistoryInformation.OptionsView.ColumnAutoWidth = false;
            grdViewHistoryInformation.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewHistoryInformation.HorzScrollVisibility = ScrollVisibility.Always;
            grdViewHistoryInformation.OptionsSelection.MultiSelect = false;
            grdViewHistoryInformation.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;

            grdCoRecordName.UnboundType = DevExpress.Data.UnboundColumnType.String;
            grdViewRecord.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            grdViewRecord.HorzScrollVisibility = ScrollVisibility.Always;
            grdViewRecord.CustomUnboundColumnData += grdViewRecord_CustomUnboundColumnData;
        }

        private void grdViewTaskCA_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void grdViewRecord_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                var tmp = (RecordEntity) e.Row;
                if (!string.IsNullOrEmpty(tmp.RecordLink))
                {
                    e.Value = "Tải xuống";
                }
                else
                {
                    
                    e.Value = null;
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (_Cus == null )
            {
                MessageBox.Show("Xin hãy chọn mẹ!");
            }
            else
            {
                string message = "";
                int tmp;
                CustomerEntity cus = new CustomerEntity();
                LoadCAFunc func = new LoadCAFunc();
                cus.Customer_ID = _Cus.Customer_ID;
                cus.FirstName = txtFirstName.Text.Trim();
                cus.LastName = txtLastName.Text.Trim();
                cus.IDNumber = txtIDNumber.Text.Trim();
                if (dtpDoBMom.EditValue != null)
                {
                    cus.DateOfBirth = (DateTime)dtpDoBMom.EditValue;
                }
                else
                {
                    cus.DateOfBirth = null;
                }
                cus.HomePhoneNumber = txtHomePhone.Text.Trim();
                cus.OfficePhoneNumber = txtOfficePhone.Text.Trim();
                cus.CellPhoneNumber = txtCellPhone.Text.Trim();
                cus.Note = txtNote.Text.Trim();
                cus.HomeNumber = txtHomeNumber.Text.Trim();
                cus.SubStreet = txtSubStreet.Text.Trim();
                cus.Street = txtStreet.Text.Trim();
                if (dtpEDD.EditValue != null)
                {
                    cus.EDD = (DateTime)dtpEDD.EditValue;
                }
                else
                {
                    cus.EDD = null;
                }
                if (int.TryParse(cmbPreviousBrand.SelectedValue.ToString(), out tmp))
                {
                    cus.PreviousBrandName = (int)cmbPreviousBrand.SelectedValue;
                }                
                cus.CurrentBrandName = (int)cmbCurrentBrand.SelectedValue;
                if (cmbDistrict.SelectedValue != null)
                {
                    cus.District_Code = (int)cmbDistrict.SelectedValue;
                }
                if (cmbWard.SelectedValue != null)
                {
                    cus.Ward_Code = (int)cmbWard.SelectedValue;
                }
                cus.Province_Code = (int)cmbProvince.SelectedValue;
                try
                {
                    func.UpdateCustomer(cus, ref message);
                }
                catch
                {
                    MessageBox.Show(message);
                }                
                MessageBox.Show("Cập nhật thành công!");
            }
            RefreshCAlist();
            grdViewTaskCA.FocusedRowHandle = _currentrow;
            GetCurrentRowData(_currentrow);
            
        }

        private void cmbDistrict_SelectionChangeCommitted(object sender, EventArgs e)
        {
            // Tự động load phường xã theo quận huyện
            int districtcode = (int)cmbDistrict.SelectedValue;
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.Parent_Code == districtcode).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
        }

        private void cmbProvince_SelectionChangeCommitted(object sender, EventArgs e)
        {
            // Tự động load quận huyện theo tỉnh thành
            int provincecode = (int)cmbProvince.SelectedValue;
            cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.Parent_Code == provincecode).ToList();
            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";
            // Tự động load phường xã theo quận huyện
            int districtcode = (int)cmbDistrict.SelectedValue;
            cmbWard.DataSource = SharedData.RegionList.Where(a => a.Parent_Code == districtcode).ToList();
            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";
        } 

        private void mbtnRefresh_Click(object sender, EventArgs e)
        {
            RefreshCAlist();
        }

        private void RefreshCAlist()
        {
            LoadCAFunc func = new LoadCAFunc();
            List<TaskListEntity> list = func.LoadCATaskList(us.UserID);
            if (list != null)
            {
                grdCACallList.DataSource = list;
            }
            grdViewTaskCA.BestFitColumns();
        }

        private void mpopRefresh_Click(object sender, EventArgs e)
        {
            RefreshCAlist();
        }

        private void btnCellPhoneCall_Click(object sender, EventArgs e)
        {
            int[] index = grdViewTaskCA.GetSelectedRows();
            var data = (TaskListEntity)grdViewTaskCA.GetRow(index[0]);
            if (data.CellPhoneNumber == null || data.CellPhoneNumber.Trim() == "")
            {
                MessageBox.Show("Không thực hiện cuộc gọi được vì chưa có số điện thoại di động");
            }
            else
            {
                string CellPhoneNumber = data.CellPhoneNumber.Trim();
                call.DialNumber(CellPhoneNumber);
            }
        }

        private void btnHomePhoneCall_Click(object sender, EventArgs e)
        {
            int[] index = grdViewTaskCA.GetSelectedRows();
            var data = (TaskListEntity)grdViewTaskCA.GetRow(index[0]);
            if (data.HomePhoneNumber == null || data.HomePhoneNumber.Trim() == "")
            {
                MessageBox.Show("Không thực hiện cuộc gọi được vì chưa có số điện thoại nhà");
            }
            else
            {
                string HomePhoneNumber = data.HomePhoneNumber.Trim();
                call.DialNumber(HomePhoneNumber);
            }
        }

        private void btnOfficePhoneCall_Click(object sender, EventArgs e)
        {
            int[] index = grdViewTaskCA.GetSelectedRows();
            var data = (TaskListEntity)grdViewTaskCA.GetRow(index[0]);
            if (data.OfficePhoneNumber == null || data.OfficePhoneNumber.Trim() == "")
            {
                MessageBox.Show("Không thực hiện cuộc gọi được vì chưa có số điện thoại công ty");
            }
            else
            {
                string OfficePhoneNumber = data.OfficePhoneNumber.Trim();
                call.DialNumber(OfficePhoneNumber);
            }
        }

        
        private void grdViewTaskCA_RowClick(object sender, RowClickEventArgs e)
        {
            _currentrow = e.RowHandle;
            GetCurrentRowData(e.RowHandle);            
        }

        private void IntialLoad()
        {
            cmbCurrentBrand.DisplayMember = "Brand_Code";
            cmbCurrentBrand.ValueMember = "Brand_ID";

            cmbPreviousBrand.DisplayMember = "Brand_Code";
            cmbPreviousBrand.ValueMember = "Brand_ID";

            cmbProvince.DisplayMember = "RegionName";
            cmbProvince.ValueMember = "Region_Code";

            cmbDistrict.DisplayMember = "RegionName";
            cmbDistrict.ValueMember = "Region_Code";

            cmbWard.DisplayMember = "RegionName";
            cmbWard.ValueMember = "Region_Code";

            cmbResultGroup.DisplayMember = "Result_Code";
            cmbResultGroup.ValueMember = "ResultID";

            cmbResultDetail.Properties.DisplayMember = "Result_Code";
            cmbResultDetail.Properties.ValueMember = "ResultID";

            colProvince.DataSource = SharedData.RegionList;
            colProvince.DisplayMember = "RegionName";
            colProvince.ValueMember = "Region_Code";
            colDistrict.DataSource = SharedData.RegionList;
            colDistrict.DisplayMember = "RegionName";
            colDistrict.ValueMember = "Region_Code";
            colWard.DataSource = SharedData.RegionList;
            colWard.DisplayMember = "RegionName";
            colWard.ValueMember = "Region_Code";
            colCollector.DataSource = SharedData.CollectorList;
            colCollector.DisplayMember = "Collector_Code";
            colCollector.ValueMember = "Collector_ID";
            colCurrentBrand.DataSource = SharedData.BrandList;
            colCurrentBrand.DisplayMember = "Brand_Code";
            colCurrentBrand.ValueMember = "Brand_ID";
            colPreviousBrand.DataSource = SharedData.BrandList;
            colPreviousBrand.DisplayMember = "Brand_Code";
            colPreviousBrand.ValueMember = "Brand_ID";
        }

        private void OnRowClicked()
        {
            cmbCurrentBrand.DataSource = SharedData.BrandList.Where(a => a.BrandLevel != 1).ToList();
            cmbPreviousBrand.DataSource = SharedData.BrandList.Where(a => a.BrandLevel != 1).ToList();
            cmbProvince.DataSource = SharedData.RegionList.Where(a => a.RegionLevel == 2).ToList();
        }

        private void ClearDataSource()
        {
            // Gia tri null thong tin cuoc goi
            cmbResultGroup.DataSource = null;
            cmbResultDetail.Properties.DataSource = null;
            cmbResultDetail.EditValue = null;
            txtNoteCall.Text = null;
            // Trả Sample về giá trị null
            cmbBooklet.DataSource = null;
            cmbSachet.DataSource = null;
        }

        private void ShowRecord()
        {
            grdRecord.DataSource = cm.getHistoryCall(_Cus.Customer_ID);
            
            grdViewRecord.BestFitColumns();            
        }

        private List<SampleDetailEntity> data;

        private void ShowSample()
        {
            data = cm.getListSample(_Cus.Customer_ID);
            grdSample.DataSource = this.BindingContext[data];
            grdSample.DataSource = data;
        }


        private bool CheckResultCall()
        {
            if (txtCallType.Text == "")
            {
                MessageBox.Show("Xin hãy chọn mẹ!");
                return false;
            }

            if (ckbEndStatus.Checked == true && (cmbResultGroup.SelectedValue == null || cmbResultDetail.EditValue == null))
            {
                MessageBox.Show("Xin chọn \"Kết quả nhóm cuộc gọi!\"");
                return false;
            }
            return true;

        }

        private CallDetailEntity ExecuteCallDetail()
        {
            string message = "";
            if (cmbResultGroup.SelectedValue != null && cmbResultDetail.EditValue != null)
            {
                CallDetailEntity cde = new CallDetailEntity();
                cde.Customer_ID = _Cus.Customer_ID;
                cde.User_ID = us.UserID;
                cde.CallStatus_ID = _CallStatus;
                cde.ResultGroup = (int)cmbResultGroup.SelectedValue;
                cde.ResultDetail = (int)cmbResultDetail.EditValue;
                cde.Note = txtNoteCall.Text.Trim();
                cde.EndStatus = ckbEndStatus.Checked;
                var tmp = cm.ExecuteCall(cde, ref message);
                if (tmp == null)
                {
                    MessageBox.Show(message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                return tmp;
            }
            else
            {
                MessageBox.Show("Chưa chọn Kết quả cuộc gọi", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (CheckResultCall() != true) return;

            if (ckbEndStatus.Checked == true)
            {
                if (cmbResultDetail.Text == "S" || cmbResultDetail.Text == "E")
                {
                    if (CheckSurvey() == false) return;
                    if (CheckInfo() == false) return;

                    _calldetail = ExecuteCallDetail();
                    if (_calldetail != null)
                    {
                        cm.InsertSurvey(byte.Parse(cmbSurCup.Text), ckbSurName.Checked, int.Parse(txtSurPrice.Text),
                             cmbSurType.SelectedItem.ToString(), txtSurEvent.Text, txtSurSampleDescription.Text, _calldetail.Call_ID);

                        if (cmbBooklet.SelectedValue != null) cm.InsertSampleDetail(cmbBooklet.Text, _calldetail.Call_ID, _Cus.Customer_ID, us.UserID);
                        if (cmbSachet.SelectedValue != null) cm.InsertSampleDetail(cmbSachet.Text, _calldetail.Call_ID, _Cus.Customer_ID, us.UserID);
                        // Cập nhật quá trình mãu gửi
                        cm.UpdateSampleDetail(data);
                        MessageBox.Show("Xác nhận thành công!");
                    }

                }
                else
                {
                    _calldetail = ExecuteCallDetail();
                    if (_calldetail != null)
                    {
                        if (cmbBooklet.SelectedValue != null) cm.InsertSampleDetail(cmbBooklet.Text, _calldetail.Call_ID, _Cus.Customer_ID, us.UserID);
                        if (cmbSachet.SelectedValue != null) cm.InsertSampleDetail(cmbSachet.Text, _calldetail.Call_ID, _Cus.Customer_ID, us.UserID);
                        // Cập nhật quá trình mãu gửi
                        cm.UpdateSampleDetail(data);
                        MessageBox.Show("Xác nhận thành công!");
                    }
                }
            }
            else
            {
                _calldetail = ExecuteCallDetail();
                if (_calldetail != null)
                {
                    MessageBox.Show("Xác nhận thành công!\n Các mẫu gửi và cập nhật tình trạng mẫu gửi sẽ không được lưu! Vì cuộc gọi này chưa phải là cuộc gọi hoàn chỉnh.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

            // Sau khi lưu trả tất cả ô trên form về null
            cmbSachet.DataSource = null;
            cmbBooklet.DataSource = null;
            txtCallType.Text = null;
            cmbResultGroup.SelectedValue = -1;
            cmbResultDetail.EditValue = null;
            txtNoteCall.Text = null;
            ckbEndStatus.Checked = false;
            // Refresh danh sách cuộc gọi
            if (_calldetail != null)
            {
                grdViewTaskCA.DeleteRow(_currentrow);
            }
            // Refresh Sample
            grdSample.DataSource = null;
        }        

        private bool CheckSurvey()
        {
            int i = 0;
            bool check = false;
            if (cmbSurCup.Text.Length > 0) { i++; }
            if (ckbSurName.Checked == true) { i++; }
            if (txtSurPrice.Text.Length > 0) { i++; }
            if (cmbSurType.Text.Length > 0) { i++; }
            if (txtSurEvent.Text.Length > 0) { i++; }
            if (txtSurSampleDescription.Text.Length > 0) { i++; }
            if (i > 3) { check = true; }
            if (check != true) MessageBox.Show("Để cuộc gọi thành công phải có ít nhất 4 thông tin trong Survey");
            return check;
        }


        private bool CheckInfo()
        {
            if (_Cus.EDD <= DateTime.Now || _Cus.EDD == null)
            {
                MessageBox.Show("Vui lòng kiểm tra ngày dự sinh!");
                return false;
            }

            return true;
        }

        private void cmbBooklet_Click(object sender, EventArgs e)
        {
            cmbBooklet.DataSource = cm.getBooklet();
            cmbBooklet.DisplayMember = "Sample_Code";
            cmbBooklet.ValueMember = "Sample_ID";
        }

        private void cmbSachet_Click(object sender, EventArgs e)
        {
            cmbSachet.DataSource = cm.getSachet();
            cmbSachet.DisplayMember = "Sample_Code";
            cmbSachet.ValueMember = "Sample_ID";
        }

        private void cmbResultGroup_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbResultDetail.Properties.DataSource = GetResultStatusDetail((int)cmbResultGroup.SelectedValue);

            CheckForm();
        }

        private List<ResultStatusEntity> GetResultStatusDetail(int value)
        {
            List<ResultStatusEntity> list = new List<ResultStatusEntity>();
            //if (_CallType == 2)
            //{
            //    list = listRSE.Where(a => a.Parent_Code == value && a.Result_Code != "E").ToList();
            //}
            //else if (_CallType == 3 || _CallType == 3)
            //{
            //    list = listRSE.Where(a => a.Parent_Code == value && a.Result_Code != "UP_User" && a.Result_Code != "E").ToList();
            //}
            //else if (_CallType == 4)
            //{
            //    list = listRSE.Where(a => a.Parent_Code == value && a.Result_Code != "UP_User" && a.Result_Code != "S").ToList();
            //}
            list = listRSE.Where(a => a.Parent_Code == value).ToList();
            return list;
        }

        private void grdViewRecord_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            var data = (RecordEntity) grdViewRecord.GetRow(e.RowHandle);
            if (e.Column.Name == "grdCoRecordName")
            {
                if (!string.IsNullOrEmpty(data.RecordLink))
                {
                    cm.DownloadFileRecord(data.RecordLink);
                    MessageBox.Show("Đã download xong!!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                }               
                
            }
        }        

        private void ckbOpenSample_CheckedChanged(object sender, EventArgs e)
        {
            CheckForm();
        }

        private void cmbResultDetail_EditValueChanged(object sender, EventArgs e)
        {
            CheckForm();
        }

        private void CheckForm()
        {
            xtcDetail.Hide();
            xtpSurvey.PageVisible = false;
            xtpSachet_Booklet.PageVisible = false;
            if (cmbResultGroup.Text == "User")
            {
                if (cmbResultDetail.Text == "S" || cmbResultDetail.Text == "E")
                {
                    xtcDetail.Show();
                    xtpSurvey.PageVisible = true;
                }
            }
            if (ckbOpenSample.Checked)
            {
                xtcDetail.Show();
                xtpSachet_Booklet.PageVisible = true;
            }
        }

        private void grcHistoryInformation_DoubleClick(object sender, EventArgs e)
        {
            sidebarCallInfor_HistoryInfor.SplitterDistance = 22;
            // Lấy lịch sử cập nhật thông tin
            if (_Cus != null)
            {
                List<HistoricalDataEntity> hd = cm.getHistoryInformation(_Cus.Customer_ID);
                if (hd != null)
                {
                    grdHistoryCustomerInformation.DataSource = hd;
                }
            }
        }

        private void grcCallInfo_DoubleClick(object sender, EventArgs e)
        {
            sidebarCallInfor_HistoryInfor.SplitterDistance = 320;
        }

        private void cmbProvince_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbProvince.SelectedValue != null)
            {
                int cusprovincecode = (int)cmbProvince.SelectedValue;
                cmbDistrict.DataSource = SharedData.RegionList.Where(a => a.Parent_Code == cusprovincecode).ToList();
            }
        }

        private void cmbDistrict_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbDistrict.SelectedValue != null)
            {
                int districtcode = (int)cmbDistrict.SelectedValue;
                cmbWard.DataSource = SharedData.RegionList.Where(a => a.Parent_Code == districtcode).ToList();
            }
        }

        private void dtpDoBChild_MouseDown(object sender, MouseEventArgs e)
        {
            if (dtpDoBChild.Value != dtpDoBChild.MinDate)
            {
                dtpDoBChild.Format = DateTimePickerFormat.Custom;
                dtpDoBChild.CustomFormat = "dd/MM/yyyy";
            }
            else
            {
                dtpDoBChild.CustomFormat = " ";
            }
        }

        private void dtpEDD_EditValueChanged(object sender, EventArgs e)
        {
            if (dtpEDD.EditValue != null)
            {
                txtEDDM.Text = HelperClass.GetEDDM((Nullable<DateTime>) dtpEDD.EditValue).ToString();
                txtEDDS.Text = HelperClass.GetEDDS((Nullable<DateTime>)dtpEDD.EditValue);
            }            
        } 


        private void CheckNumber(TextEdit textbox)
        {
            LoadCustomerFunc func = new LoadCustomerFunc();
            switch (textbox.Name)
            {                    
                case "txtCellPhone":                    
                    if (func.LoadCustomerByCellPhone(textbox.Text) != null)
                    {
                        MessageBox.Show("Số này đã tồn tại\n" + "Xin hãy nhập số khác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //append the old number
                        textbox.EditValue = textbox.OldEditValue;
                        textbox.Focus();
                    }
                    else if (textbox.Text == "")
                    {
                        textbox.EditValue = textbox.OldEditValue;
                    }
                    break;

                case "txtHomePhone":
                    if (func.LoadCustomerByHomePhone(textbox.Text) != null)
                    {
                        MessageBox.Show("Số này đã tồn tại\n" + "Xin hãy nhập số khác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //append the old number
                        textbox.EditValue = textbox.OldEditValue;
                        textbox.Focus();
                    }
                    else if (textbox.Text == "")
                    {
                        textbox.EditValue = textbox.OldEditValue;
                    }
                    break;

                case "txtOfficePhone":
                    if (func.LoadCustomerByOfficePhone(textbox.Text) != null)
                    {
                        MessageBox.Show("Số này đã tồn tại\n" + "Xin hãy nhập số khác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //append the old number
                        textbox.EditValue = textbox.OldEditValue;
                        textbox.Focus();
                    }
                    else if (textbox.Text == "")
                    {
                        textbox.EditValue = textbox.OldEditValue;
                    }
                    break;
            }
        }


        private void txtCellPhone_Leave(object sender, EventArgs e)
        {
            if (_Cus != null)
            {
                if (_Cus.CellPhoneNumber != txtCellPhone.Text)
                {
                    CheckNumber(txtCellPhone);
                }
            }
        }

        private void txtHomePhone_Leave(object sender, EventArgs e)
        {
            if (_Cus != null)
            {
                if (_Cus.HomePhoneNumber != txtHomePhone.Text)
                {
                    CheckNumber(txtHomePhone);
                }
            }
        }

        private void txtOfficePhone_Leave(object sender, EventArgs e)
        {
            if (_Cus != null)
            {
                if (_Cus.OfficePhoneNumber != txtOfficePhone.Text)
                {
                    CheckNumber(txtOfficePhone);
                }
            }
        } 

        private void txtSurPrice_Leave(object sender, EventArgs e)
        {
            if (txtSurPrice.EditValue != null)
            {
                if (int.Parse(txtSurPrice.EditValue.ToString()) > 2000000000)
                {
                    MessageBox.Show("Giá tiền không thể quá lớn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtSurPrice.Focus();
                }
            }
        }

        private void GetCurrentRowData(int RowHandle)
        {
            LoadCustomerFunc func = new LoadCustomerFunc();
            var task = (TaskListEntity)grdViewTaskCA.GetRow(RowHandle);
            _Cus = func.LoadCustomerById(task.CustomerID);
            _CallStatus = task.CallStatusID.Value;
            bsCus.DataSource = _Cus;
            OnRowClicked();
            // Thong tin me
            txtLastName.Text = _Cus.LastName;
            txtFirstName.Text = _Cus.FirstName;

            txtIDNumber.Text = _Cus.IDNumber;
            txtNote.Text = _Cus.Note;
            txtHomePhone.Text = _Cus.HomePhoneNumber;
            txtCellPhone.Text = _Cus.CellPhoneNumber;
            txtOfficePhone.Text = _Cus.OfficePhoneNumber;
            if (_Cus.DateOfBirth != null)
            {
                dtpDoBMom.EditValue = (DateTime)_Cus.DateOfBirth;
            }
            else
            {
                dtpDoBMom.EditValue = null;
            }
            cmbCurrentBrand.SelectedValue = _Cus.CurrentBrandName;

            if (_Cus.PreviousBrandName == null)
            {
                cmbPreviousBrand.SelectedValue = 15;
            }
            else
            {
                cmbPreviousBrand.SelectedValue = _Cus.PreviousBrandName;
            }

            // Dia chi
            txtHomeNumber.Text = _Cus.HomeNumber;
            txtSubStreet.Text = _Cus.SubStreet;
            txtStreet.Text = _Cus.Street;
            // Quan, huyen, tinh thanh
            if (_Cus.Province_Code != null) cmbProvince.SelectedValue = _Cus.Province_Code;

            if (_Cus.District_Code != null) cmbDistrict.SelectedValue = _Cus.District_Code;
            if (_Cus.Ward_Code != null) cmbWard.SelectedValue = _Cus.Ward_Code;

            //// Thong tin cuoc goi
            txtCallType.Text = task.CallStatus;
            // Thong tin con
            if (_CallStatus == 2 || _CallStatus == 3)
            {
                grbAddChild.Hide();
                grbChildInformation.Hide();
            }
            else
            {
                grbAddChild.Show();
                grbChildInformation.Show();
            }
            // Du Sinh
            if (_Cus.EDD == null)
            {
                dtpEDD.EditValue = null;
                txtEDDM.Text = null;
                txtEDDS.Text = null;
            }
            else
            {
                dtpEDD.EditValue = (DateTime)_Cus.EDD;
                txtEDDM.Text = HelperClass.GetEDDM((DateTime)_Cus.EDD).ToString();
                txtEDDS.Text = HelperClass.GetEDDS(_Cus.EDD);
            }
            // Tự động load Kết quả nhóm cuộc gọi

            if (_CallStatus == 2 && _Cus.CurrentBrandName != 14)
            {
                cmbResultGroup.DataSource = cm.GetResultStatusGroup(_CallStatus).Where(a => a.Result_Code != "User").ToList();
            }
            else
            {
                cmbResultGroup.DataSource = cm.GetResultStatusGroup(_CallStatus);
            }

            // Tự động load lịch sử cuộc gọi
            ShowRecord();
            ShowSample();
        }


    }
}